package com.example.group.network.express_plus_app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.group.network.express_plus_app.Dao.AppDatabase;
import com.example.group.network.express_plus_app.Dao.UserDao.UserDao;
import com.example.group.network.express_plus_app.bean.MyResponse;
import com.example.group.network.express_plus_app.bean.Token;
import com.example.group.network.express_plus_app.data.model.LoggedInUser;
import com.example.group.network.express_plus_app.http.GsonRequest;
import com.example.group.network.express_plus_app.http.MySingleton;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UpdateNetNameActivity extends AppCompatActivity {
    String server_ip;

    @BindView(R.id.et_up_net_name)
    EditText et_up_net_name;
    RequestQueue queue;
    UserDao userDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_net_name);
        server_ip= getApplication().getString(R.string.server_ip);
        ButterKnife.bind(this);
        userDao=AppDatabase.getsInstance(getApplicationContext()).userDao();
        setActionBar("设置用户名");
        queue= MySingleton.getInstance(getApplicationContext()).getRequestQueue();
        findViewById(R.id.btn_net_name_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateNetName();
            }
        });
    }

    public void updateNetName(){
        HashMap<String, String> mInfoMap = MainApplication.getInstance().mInfoMap;
        String token = mInfoMap.get("token");
        String phone = mInfoMap.get("phone");
        String net_name = et_up_net_name.getText().toString();
        Map<String,String> head=new HashMap<>();
        head.put("Authorization","Bearer "+token);
        String update_net_name_url=server_ip+"express-user/user-controller/update-user-net-name?";
        GsonRequest<MyResponse> tokenGsonRequest=new GsonRequest<MyResponse>(update_net_name_url, Request.Method.POST, MyResponse.class, head, new Response.Listener<MyResponse>() {
            @Override
            public void onResponse(MyResponse response) {
                //更改数据库
                if (response.getOk()){
                    Log.i("response",response.toString());
                    LoggedInUser loggedInUser = new LoggedInUser(phone,net_name,token,mInfoMap.get("user_face"),mInfoMap.get("delivery_address"));
                    userDao.updateUserDeliveryAddress(loggedInUser);
                    mInfoMap.put("net_name",net_name);
                    Toast.makeText(UpdateNetNameActivity.this, "更改成功", Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(UpdateNetNameActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> parms=new HashMap<>();
                parms.put("userPhone",phone);
                parms.put("userNetName",net_name);
                return parms;
            }
        };
        tokenGsonRequest.setRetryPolicy(new DefaultRetryPolicy(6000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(tokenGsonRequest);
    }

    public void setActionBar(String title){
        //标题栏设置
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_ios_24);
        supportActionBar.setTitle(title);

    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}